import os
import logging
from logging.handlers import TimedRotatingFileHandler

class Logger:
    def __init__(self, log_dir='Logs', logger_name='app',fmt='%(asctime)s %(name)s [%(levelname)s]: %(message)s'):
        os.makedirs(log_dir,exist_ok=True)
        self.logger = logging.getLogger(logger_name)
        self.logger.setLevel(logging.DEBUG)
        # 格式
        formatter = logging.Formatter(fmt)
        
        # 创建控制台处理器
        console_handler = logging.StreamHandler()
        console_handler.setLevel(logging.DEBUG)
        console_handler.setFormatter(formatter)
        self.logger.addHandler(console_handler)

        # 创建日志文件处理器
        log_file_handler = TimedRotatingFileHandler(
            os.path.join(log_dir, f"{logger_name}.log"), 
            encoding='UTF-8',
            when='midnight', 
            interval=1, 
            backupCount=14
        )
        log_file_handler.setLevel(logging.DEBUG)
        log_file_handler.setFormatter(formatter)
        self.logger.addHandler(log_file_handler)

    def debug(self, msg):
        self.logger.debug(msg)

    def info(self, msg):
        self.logger.info(msg)

    def warning(self, msg):
        self.logger.warning(msg)

    def error(self, msg):
        self.logger.error(msg)

    def critical(self, msg):
        self.logger.critical(msg)

# 使用案例
if __name__ == "__main__":
    logger = Logger()
    logger.debug("调试日志")
    logger.info("测试日志")
    logger.warning("警告日志")
    logger.error("错误日志")
    logger.critical("严重错误日志")